/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package unsch.secretaria.servlet;

import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONObject;
import unsch.secretaria.bean.BEscuela;
import unsch.secretaria.bean.BExpediente;
import unsch.secretaria.controller.CONTROLLEREscuela;
import unsch.secretaria.controller.CONTROLLERExpediente;
import unsch.secretaria.dao.DAOExpediente;
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONObject;

/**
 *
 * @author davis
 */
@WebServlet(name = "SConsultarExpediente", urlPatterns = {"/SConsultarExpediente"})
public class SConsultarExpediente extends HttpServlet {

    private static final long serialVersionUID = 1L;

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    public SConsultarExpediente() {
        super();
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
//            /* TODO output your page here. You may use following sample code. */
//            out.println("<!DOCTYPE html>");
//            out.println("<html>");
//            out.println("<head>");
//            out.println("<title>Servlet SConsultarExpediente</title>");            
//            out.println("</head>");
//            out.println("<body>");
//            out.println("<h1>Servlet SConsultarExpediente at " + request.getContextPath() + "</h1>");
//            out.println("</body>");
//            out.println("</html>");
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
        StringBuffer jb = new StringBuffer();
        String line = null;
        try {
            BufferedReader reader = request.getReader();
            while ((line = reader.readLine()) != null) {
                jb.append(line);
            }
            //System.out.println(jb.toString());
            String datt = String.valueOf(jb.toString());

            JSONObject jsonObj = new JSONObject(datt);

            BExpediente oBExpediente = new BExpediente();
            String tipo = (String) jsonObj.get("tipoExpediente");
            oBExpediente.setTipoExpediente("Titulo Profesional");

            String estado = (String) jsonObj.get("estadoExpediente");
            oBExpediente.setEstado("aprobado");

            BEscuela oBEscuela = new BEscuela();
            //String idEscuela = (String) jsonObj.get("escuelaExpediente");
            oBEscuela.setIdEscuela(1);

            String json1 = new Gson().toJson(new CONTROLLERExpediente().listarExpedientes(oBExpediente, oBEscuela));
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            response.getWriter().write(json1);
        } catch (Exception e) {
        }
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        StringBuffer jb = new StringBuffer();
        String line = null;
        try {
            BufferedReader reader = request.getReader();
            while ((line = reader.readLine()) != null) {
                jb.append(line);
            }
            //System.out.println(jb.toString());
            String datt = String.valueOf(jb.toString());

            JSONObject jsonObj = new JSONObject(datt);


            String parametro = (String) jsonObj.get("parametro");


            if (parametro.equals("tipo")) {
                String json1 = new Gson().toJson(new CONTROLLERExpediente().listarTipoExpediente());
                response.setContentType("application/json");
                response.setCharacterEncoding("utf-8");
                response.getWriter().write(json1);
            }
            if (parametro.equals("estado")) {
                String json1 = new Gson().toJson(new CONTROLLERExpediente().listarEstadoExpediente());
                response.setContentType("application/json");
                response.setCharacterEncoding("utf-8");
                response.getWriter().write(json1);
            }
            if (parametro.equals("escuela")) {
                String json1 = new Gson().toJson(new CONTROLLEREscuela().listarEscuela());
                response.setContentType("application/json");
                response.setCharacterEncoding("utf-8");
                response.getWriter().write(json1);
            }
            if (parametro.equals("expediente")) {
                BExpediente oBExpediente = new BExpediente();
                String tipo = (String) jsonObj.get("tipoExpediente");
                oBExpediente.setTipoExpediente(tipo);

                String estado = (String) jsonObj.get("estadoExpediente");
                oBExpediente.setEstado(estado);

                BEscuela oBEscuela = new BEscuela();
                String idEscuela = (String) jsonObj.get("escuelaExpediente");
                oBEscuela.setIdEscuela(Integer.parseInt(idEscuela));
                String json1 = new Gson().toJson(new CONTROLLERExpediente().listarExpedientes(oBExpediente, oBEscuela));
                response.setContentType("application/json");
                response.setCharacterEncoding("utf-8");
                response.getWriter().write(json1);
            }
        } catch (Exception e) {
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
